﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;

namespace GCPUD.Autoprov.Network
{
    public class Mac : IComparable
    {
        private byte[] _mac = new byte[6];
        public byte[] mac
        {
            get { return _mac; }
            set { }
        }
        public Mac()
        { }

        public Mac(string macAddress)
        {
            if (Mac.IsValid(macAddress))
            {
                this.Parse(macAddress);
            }
        }
        public void Parse(string macAddress)
        {
            macAddress = macAddress.Trim();
            //List<string> section = new List<string>();
            //section = macAddress.Split('.').ToList();
            List<string> parts = new List<string>();
            //Debug.WriteLine(macAddress);
            Regex ciscoExp = new Regex(@"^[0-9,A-F][0-9,A-F][0-9,A-F][0-9,A-F]\.[0-9,A-F][0-9,A-F][0-9,A-F][0-9,A-F]\.[0-9,A-F][0-9,A-F][0-9,A-F][0-9,A-F]$", RegexOptions.IgnoreCase);
            Match match = ciscoExp.Match(macAddress);
            if (match.Success)
            {
                parts.Add(macAddress.Substring(0, 2));
                parts.Add(macAddress.Substring(2, 2));
                parts.Add(macAddress.Substring(5, 2));
                parts.Add(macAddress.Substring(7, 2));
                parts.Add(macAddress.Substring(10, 2));
                parts.Add(macAddress.Substring(12, 2));
            }
            Regex plainExp = new Regex(@"^[0-9,A-F][0-9,A-F][0-9,A-F][0-9,A-F][0-9,A-F][0-9,A-F][0-9,A-F][0-9,A-F][0-9,A-F][0-9,A-F][0-9,A-F][0-9,A-F]$", RegexOptions.IgnoreCase);
            match = plainExp.Match(macAddress);
            if (match.Success)
            {
                parts.Add(macAddress.Substring(0, 2));
                parts.Add(macAddress.Substring(2, 2));
                parts.Add(macAddress.Substring(4, 2));
                parts.Add(macAddress.Substring(6, 2));
                parts.Add(macAddress.Substring(8, 2));
                parts.Add(macAddress.Substring(10, 2));
            }
            Regex colSepExp = new Regex(@"^[0-9,A-F][0-9,A-F]:[0-9,A-F][0-9,A-F]:[0-9,A-F][0-9,A-F]:[0-9,A-F][0-9,A-F]:[0-9,A-F][0-9,A-F]:[0-9,A-F][0-9,A-F]$", RegexOptions.IgnoreCase);
            match = colSepExp.Match(macAddress);
            if (match.Success)
            {
                parts.Add(macAddress.Substring(0, 2));
                parts.Add(macAddress.Substring(3, 2));
                parts.Add(macAddress.Substring(6, 2));
                parts.Add(macAddress.Substring(9, 2));
                parts.Add(macAddress.Substring(12, 2));
                parts.Add(macAddress.Substring(15, 2));
            }

            _mac[0] = Convert.ToByte(parts.ElementAt(0), 16);
            _mac[1] = Convert.ToByte(parts.ElementAt(1), 16);
            _mac[2] = Convert.ToByte(parts.ElementAt(2), 16);
            _mac[3] = Convert.ToByte(parts.ElementAt(3), 16);
            _mac[4] = Convert.ToByte(parts.ElementAt(4), 16);
            _mac[5] = Convert.ToByte(parts.ElementAt(5), 16);

        }
        public override int GetHashCode()
        {
            return this._mac.GetHashCode();
        }
        public override bool Equals(Object obj)
        {
            bool retVal = false;
            if (obj != null)
            {

                Mac macObj = (Mac)obj;

                if ((macObj._mac[0] == this._mac[0])
                    &&
                    (macObj._mac[1] == this._mac[1])
                    &&
                    (macObj._mac[2] == this._mac[2])
                    &&
                    (macObj._mac[3] == this._mac[3])
                    &&
                    (macObj._mac[4] == this._mac[4])
                    &&
                    (macObj._mac[5] == this._mac[5])

                    )
                    retVal = true;
            }
            return retVal;
        }
        public int CompareTo(object obj)
        {
            Mac otherMac = obj as Mac;
            if (otherMac != null)
                return this.mac.ToString().CompareTo(otherMac.mac.ToString());
            else
                throw new ArgumentException("Object is not a Mac");
        }

        public override string ToString()
        {
            string information = string.Format("{0}:{1}:{2}:{3}:{4}:{5}",
                        mac[0].ToString("X2"), mac[1].ToString("X2"),
                        mac[2].ToString("X2"), mac[3].ToString("X2"),
                        mac[4].ToString("X2"), mac[5].ToString("X2"));
            return information;
        }
        public string ToOid()
        {
            string oct1 = Convert.ToInt32(mac[0]).ToString();
            string oct2 = Convert.ToInt32(mac[1]).ToString();
            string oct3 = Convert.ToInt32(mac[2]).ToString();
            string oct4 = Convert.ToInt32(mac[3]).ToString();
            string oct5 = Convert.ToInt32(mac[4]).ToString();
            string oct6 = Convert.ToInt32(mac[5]).ToString();
            return oct1 + "." + oct2 + "." + oct3 + "." + oct4 + "." + oct5 + "." + oct6;
        }
        static public bool IsValid(string macToCheck)
        {
            macToCheck = macToCheck.Trim();
            Regex ciscoExp = new Regex(@"^[0-9,A-F][0-9,A-F][0-9,A-F][0-9,A-F]\.[0-9,A-F][0-9,A-F][0-9,A-F][0-9,A-F]\.[0-9,A-F][0-9,A-F][0-9,A-F][0-9,A-F]$", RegexOptions.IgnoreCase);
            Match match = ciscoExp.Match(macToCheck);
            if (match.Success)
            {
                return true;
            }
            Regex plainExp = new Regex(@"^[0-9,A-F][0-9,A-F][0-9,A-F][0-9,A-F][0-9,A-F][0-9,A-F][0-9,A-F][0-9,A-F][0-9,A-F][0-9,A-F][0-9,A-F][0-9,A-F]$", RegexOptions.IgnoreCase);
            match = plainExp.Match(macToCheck);
            if (match.Success)
            {
                return true;
            }
            Regex colSepExp = new Regex(@"^[0-9,A-F][0-9,A-F]:[0-9,A-F][0-9,A-F]:[0-9,A-F][0-9,A-F]:[0-9,A-F][0-9,A-F]:[0-9,A-F][0-9,A-F]:[0-9,A-F][0-9,A-F]$", RegexOptions.IgnoreCase);
            match = colSepExp.Match(macToCheck);
            if (match.Success)
            {
                return true;
            }
            return false;
        }
        static public bool IsValid(Mac macToCheck)
        {
            return IsValid(macToCheck.ToString());
        }

    }
}
